import { auth } from 'utils/auth.js';
import { get } from 'utils/request.js';
import { config } from 'config.js';


var app = getApp();
var timer = -1;
var upTimer = -1;
let configs = config();
Page({
    data: {
        lists: [
        ],
        requestUrl: configs.complainList,
        current_page: 1,
        total: 0,
        totalCount: 0,
        isEmpty: true,
        isLoadData: true,
        pageStatus: false,
        onLine: true,
        isShow: false
    },
    onLoad: function(options) {

      var data = {
        current_page: 1,
        page_size: 10
      }
      this.http(this.data.requestUrl, data, this.processData);
    },
    processData(tempData) {
    
        if (tempData.ret == 0) {
            this.data.total = tempData.data.pagination.total;

            if (tempData.data.list.length > 0) {
                var lists = [];
                for (var idx in tempData.data.list) {
                    var list = tempData.data.list[idx];
                    
                    var temp = {
                        status_text: list.status_text,
                        create_time: list.create_time,
                        name: list.name,
                        status: list.status,
                        id: list.id
                    }
                    lists.push(temp);
                }
                //上拉加载
                var totalLists = [];
                //如果要绑定新加载的数据，那么需要把旧数据合并到一起
                if (!this.data.isEmpty) {
                    totalLists = this.data.lists.concat(lists);
                } else {
                    totalLists = lists;
                    this.data.isEmpty = false;
                }
                console.log(totalLists);
                this.setData({
                    lists: totalLists
                });
                this.data.current_page += 1;
                this.data.totalCount += parseInt(tempData.data.pagination.page_size);
            } else {
                this.setData({
                    isShow: true
                });
            }

        }else{
            this.setData({
                onLine: false
            });
        }
        wx.hideLoading();
        // wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
    },
    onReady(e) {
    },
    onScrollLower(e) {
        var that = this;
        clearTimeout(timer);

        console.log('onScrollLower')
        console.log(that.data.current_page)
        
        timer = setTimeout(function() {

            if (that.data.total >= that.data.totalCount) {
                var data = {
                  current_page: that.data.current_page,
                  page_size: 10
                }
                var nextUrl = that.data.requestUrl;
                that.http(nextUrl, data, that.processData);
                wx.showToast({
                  title: '加载中...',
                  icon: 'loading'
                })
                // wx.showNavigationBarLoading();
            }
        }, 300);

    },
    onScrollUpper(e){
       var that = this;
       clearTimeout(upTimer);
       upTimer = setTimeout(function(){
         that.pageRefresh();
       }, 300)
    },
    onShow(e) {
      if (this.data.pageStatus) {
          this.pageRefresh();
      }
    },
    onHide(e) {
        var that = this;
        that.data.pageStatus = true;
        that.data.current_page = 1;
    },
    pageRefresh(type) {
        var that = this;
        
        that.setData({
            'lists': [],
            'isEmpty': true,
            'totalCount': 0,
            'total': 0,
            'current_page': 1,
            onLine: true
        })

        var refreshUrl = that.data.requestUrl;

        var data = {
          current_page: 1,
          page_size: 10
        }
        that.http(refreshUrl, data, that.processData);
        // wx.showNavigationBarLoading();
    },
    http(url, data, callback) {
        var that = this;
        wx.showLoading({
          title: '加载中'
        })
        get({'path': url}, data).then(function(res){
          callback(res.data)
        }, function(res){
          that.setData({
              onLine: false
          });
          return false;
        })
    },
    goCompliaintDetailTap(e){
        var id = e.currentTarget && e.currentTarget.dataset && e.currentTarget.dataset.id;
        wx.navigateTo({
          url: `/pages/complaint-detail/complaint-detail?id=${id}`
        })
    },
    onRefreshTap(e){
        var that = this;
        that.pageRefresh();
  },
})
